import { allList, total, inAndOutTotal } from "../../server/api";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ishow: false,
    pageNo: 1,
    pageSize: 10,
    active:null,
    dataSource: [],
    show: false,
    currentDate: new Date().getTime(),
    year: "",
    month: "",
    kyjf: "", //可用积分
    jf: "", //即将到期积分
    expenditure: "", //支出
    harvest: "", //收获
    minDate: new Date(1940, 0, 1).getTime(),
    formatter(type, value) {
      if (type === "year") {
        return `${value}`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      return value;
    },
    num: "1",
    type: 1,
  },
  onChange(e) {
    this.setData({ active: e.detail.index });
    if (e.detail.index == 0) {
      this.setData({
        dataSource: [],
        pageNo: 1,
        pageSize: 10,
        type: 1,
      });
      this.getallList();
    } else if (e.detail.index == 1) {
      this.setData({
        dataSource: [],
        pageNo: 1,
        pageSize: 10,
        type: 2,
      });
      this.getallList();
    } else {
      this.setData({
        dataSource: [],
        pageNo: 1,
        pageSize: 10,
        type: 3,
      });
      this.getallList();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  //上拉加载更多事件处理函数
  getMore(e) {
    this.data.pageNo++;
    let params = {
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
    };
    if (this.data.dataSource.length - 1) {
      setTimeout(() => {
        if (this.data.active == "0") {
          this.getallList(params);
          if (this.data.dataSource.length - 1 > 10) {
            this.setData({ ishow: true });
          }
        } else if (this.data.active == "1") {
          this.getallList(params);
          if (this.data.dataSource.length - 1 > 10) {
            this.setData({ ishow: true });
          }
        } else {
          this.getallList(params);
          if (this.data.dataSource.length - 1 > 10) {
            this.setData({ ishow: true });
          }
        }
      }, 1000);
    }
  },
  //日期选择事件
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
  //取消日期
  onCancel() {
    this.setData({ show: false });
  },
  //日期确认事件
  onConfirm(val) {
    this.setData({ show: false });
    this.setData({
      year: this.timeFormat(val.detail),
      month: this.monthFormat(val.detail),
    });
    this.getinAndOutTotal();

    setTimeout(() => {
      if (this.data.active == "0") {
        this.setData({
          dataSource: [],
          pageNo: 1,
          pageSize: 10,
          type: 1,
        });
        this.getallList();
      } else if (this.data.active == "1") {
        this.setData({
          dataSource: [],
          pageNo: 1,
          pageSize: 10,
          type: 2,
        });
        this.getallList();
      } else {
        this.setData({
          dataSource: [],
          pageNo: 1,
          pageSize: 10,
          type: 3,
        });
        this.getallList();
      }
    }, 1000);
  },
  //转年
  timeFormat(time) {
    // 时间格式化 2019-09-08
    var time = new Date(time);
    let year = time.getFullYear();
    return year;
  },
  //转月
  monthFormat(time) {
    var time = new Date(time);
    let month = time.getMonth() + 1;
    return month;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},
  black() {
    if (this.data.num == 1) {
      wx.switchTab({
        url: "/pages/pointsmall/pointsmall",
      });
    } else if (this.data.num == 5) {
      wx.switchTab({
        url: "/pages/home/home",
      });
    } else {
      wx.switchTab({
        url: "/pages/my/my",
      });
    }
  },
  //获取当前日期
  getdate() {
    const now = new Date();
    this.setData({ year: now.getFullYear(), month: now.getMonth() + 1 });
    // this.setData({ active: "0", type: 1 });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log();
    const count = wx.getStorageSync('active')
    this.setData({active:count,type:count == 0 ? 1 : 3})
    this.getdate();
     this.getallList();
   
    if (wx.getStorageSync("count") == "4") {
      this.setData({ num: 4 });
    } else if (wx.getStorageSync("count") == "5") {
      this.setData({ num: 5 });
    } else {
      this.setData({ num: 1 });
    }
    this.setData({
      dataSource: [],
      pageNo: 1,
      pageSize: 10,
    });

    this.getTabBar().setData({ active: 1 });
    this.getTabBar().toggleMenu(0);
    this.getSource();
    this.getinAndOutTotal();
  },
  //获取即将到期积分
  async getSource() {
    const res = await total();
    if (res.code == 200) {
      const data = res.result;
      console.log(data, "data");
      this.setData({
        kyjf: data == null ? "0" : data.balance,
        jf: data == null ? "0" : data.adventPeriodAmount,
      });
    }
  },
  //全部积分
  async getallList(res) {
    const year = this.data.year;
    const month =
      this.data.month < 10 ? "0" + this.data.month : this.data.month;
    const yearmonth = year + "-" + month;
    const params = {
      time: yearmonth,
      pageNo: res == undefined ? this.data.pageNo : res.pageNo,
      pageSize: res == undefined ? this.data.pageSize : res.pageSize,
      type: this.data.type,
    };
    const data = await allList(params);
    const val = data.result.records.map((item)=>{
       item.time = item.createTime.replace(/-/g, '/')
       return item
    })
    if (data.code == 200) {
      this.setData({
        dataSource:
          res == undefined
            ? val
            : [...this.data.dataSource, ...val],
      });
    }
  },
  // 支出与收获
  async getinAndOutTotal() {
    const year = this.data.year;
    const month =
      this.data.month < 10 ? "0" + this.data.month : this.data.month;
    const yearmonth = year + "-" + month;
    const params = {
      time: yearmonth,
    };
    const data = await inAndOutTotal(params);
    if (data.code == 200) {
      const zcdata = data.result.in == null ? 0 : data.result.in;
      const shdata = data.result.out == null ? 0 : data.result.out;
      this.setData({ expenditure: shdata, harvest: zcdata });
    }
  },
  taptime() {
    this.setData({ show: true });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    wx.removeStorageSync("count");
    wx.removeStorageSync("active");
    this.setData({active:null})
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    return false;
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom(e) {
    // console.log(e, "e");
  },
  upper(e) {
    // console.log('请求更多数据')
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
